home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7267 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: hydro.com!usenet
  2. From: Terje Mathisen <Terje.Mathisen@hda.hydro.com>
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.msdos.programmer,comp.programming,comp.windows.ms.programmer.misc
  4. Subject: Re: Date Arithmetic
  5. Date: Thu, 22 Feb 1996 13:48:48 +0100
  6. Organization: Hydro
  7. Message-ID: <312C6630.916@hda.hydro.com>
  8. References: <4g19kp$640@tracy.protocom.com> <jrs.2754.000A7444@dclf.npl.co.uk> <4ggsj3$1fg@dopey.magg.net>
  9. NNTP-Posting-Host: vkhdpc40.hda.hydro.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14. CC: "Michael J. Karas" <mkaras@pclink.com>
  15.  
  16. Dennis Hawkins wrote:
  17. > jrs@dclf.npl.co.uk (Dr John Stockton  NPL UK) wrote:
  18. > >In article <4g19kp$640@tracy.protocom.com> "Michael J. Karas" <mkaras@pclink.com> writes:
  19. > >>I am working on an algorithm for a laser marking machine that writes
  20. > >>expiration delays on to food product boxes. The algorithm needs to
  21. > >>be able to add NNN days to todays date in the fastest manner possible
  22. > >>without using any floating point arithmetic. I could use help from anyone
  23. > This shouldn't be a big deal.  There are several algorithims to handle
  24. > this.  The best one depends on the range of NNN days.  If it is just a
  25. > couple of weeks then add it by brute force to the month.  If NNN can
  26. > go to 999 then you are probably better off converting todays date into
  27. > a julian or an integer that represents the number of days since your
  28. > machine was put on line.  Then just add the days like you would any
  29. > other integer.  Then convert it back again.  Dates can be tricky, you
  30. > just need to do a lot of testing to make sure they work ok though.
  31.  
  32. I just wrote a very simple function to do this, it uses no multiplication or 
  33. division operations, just simple logic that your laser marker should have:
  34.  
  35. On both a Pentium and a PPro cpu, it used less than 90 cycles when compiled with 
  36. Watcom C10.5, full optimization.  I tested it by calling the function with today 
  37. (1996-02-21) as the starting point, and for all offsets from 0 to 34999 days ahead.
  38.  
  39. The last day (today + 34999) seems to be 2091-12-19.
  40.  
  41. I hope you can use this!
  42.  
  43. /*
  44.    add_days_to_date() (c) Terje Mathisen 1996.
  45.    
  46.    Assumes all inputs to be OK, the year must be in 1901 to 2099
  47.    range,but no checking is done, to keep the function as fast &
  48.    simple as possible!
  49.    
  50. */
  51. int add_days_to_date(int *year, int *month, int *day, int days_to_add)
  52. {
  53.     static int days_before_month[13] = 
  54. {0,31,61,92,122,153,184,214,245,275,306,337,366};
  55.     int y = *year, m = *month - 3, d = *day - 1;
  56.  
  57.     if (m < 0) {
  58.         m += 12;
  59.         y --;
  60.     }
  61. /* Add the day number in the current year, day 0 at 1 March! */
  62.     days_to_add += days_before_month[m] + d;
  63.  
  64. /* Subtract full four-year periods: */
  65.     while (days_to_add >= 1461) {  /* Days in a 4-year period, inc. leap day */
  66.         days_to_add -= 1461;
  67.         y += 4;
  68.     }
  69.  
  70. /* Subtract full years: */
  71.     while (days_to_add >= 365) {
  72.         days_to_add -= 365;
  73.         if (y & 3 == 3) {  /* Leap year rule with offset years */
  74.             days_to_add--;
  75.             if (days_to_add < 0) {
  76.                 days_to_add = 365; /* Must be 29 Feb */
  77.                 break;
  78.             }
  79.         }
  80.         y++;
  81.     }
  82.  
  83. /* Locate the month with a table lookup, start with a (good)
  84.    approximate value::
  85. */
  86.     m = days_to_add >> 5;
  87.     if (days_to_add >= days_before_month[m+1]) m++;
  88.     *day = days_to_add - days_before_month[m] + 1;
  89.     if (m > 9) {
  90.         m -= 12;
  91.         y++;
  92.     }
  93.     *year = y;
  94.     *month = m + 3;
  95.     return 0;
  96. }
  97.  
  98.  
  99.  
  100.  
  101. -- 
  102. -Terje Mathisen (include std disclaimer) <Terje.Mathisen@hda.hydro.com>
  103. "almost all programming can be viewed as an exercise in caching"
  104.